﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using LotteryVote.Entity;
using System.Diagnostics;
using LotteryVote.Utility;

namespace LotteryVote
{
    public partial class LotteryResultManager : System.Web.UI.Page
    {
        public IEnumerable<LotteryResult> LotteryResult { get; private set; }
        public IList<CompanyTypeSupportNumLen> NumLenSupportList { get; private set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            LotteryResult = new List<LotteryResult>();
            if (!IsPostBack)
            {
                BindTodayCompany();
                //btn_getRecord_Click(this, EventArgs.Empty);
                BindCompanyRecords();
            }
        }

        void BindTodayCompany()
        {
            rbl_company.DataSource = TodayLotteryCompany.Instance.GetTodayCompany();
            rbl_company.DataTextField = LotteryCompany.NAME;
            rbl_company.DataValueField = LotteryCompany.COMPANYID;
            rbl_company.DataBind();
            rbl_company.Items[0].Selected = true;
        }

        protected void btn_GetResult_Click(object sender, EventArgs e)
        {
            LotteryResultFacade lrFacade = new LotteryResultFacade();
            DateTime date = DateTime.Parse(txt_date.Text);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //LotteryResult = lrFacade.GetLotteryResultByDate(date);
            Response.Write(sw.ElapsedMilliseconds);
            sw.Stop();
        }

        protected void btn_getRecord_Click(object sender, EventArgs e)
        {
            LotteryResultFacade lrFacade = new LotteryResultFacade();
            gv_record.DataSource = lrFacade.GetTodayRecordByCompany(int.Parse(rbl_company.SelectedValue));
            gv_record.DataBind();
        }

        protected void btn_canInput_Click(object sender, EventArgs e)
        {
            LotteryResultFacade lrFacade = new LotteryResultFacade();
            lbl_message.Text = lrFacade.CanInputRecordValue(int.Parse(rbl_company.SelectedValue)).ToString();
        }

        protected void btn_addRecord_Click(object sender, EventArgs e)
        {
            LotteryResultFacade lrFacade = new LotteryResultFacade();
            lrFacade.CompanyAddRecord(int.Parse(rbl_company.SelectedValue), txt_num.Text);
            btn_getRecord_Click(sender, e);
        }

        protected void rbl_company_SelectedIndexChanged(object sender, EventArgs e)
        {
            //btn_getRecord_Click(sender, e);
            BindCompanyRecords();
        }

        void BindCompanyRecords()
        {
            NumLenFacade numLenFacade = new NumLenFacade();
            NumLenSupportList = numLenFacade.GetNumLengthByCompanyId(int.Parse(rbl_company.SelectedValue));
        }

        protected void btn_GetJson_Click(object sender, EventArgs e)
        {
            List<LotteryRecord> recordList = new List<LotteryRecord>();
            for (int i = 1; i < 27; i++)
            {
                recordList.Add(new LotteryRecord { Value = i.ToString() });
            }
            string json = JSONHelper.ToJson(recordList);
            Response.Write(json);
        }

        protected void btn_AddNums_Click(object sender, EventArgs e)
        {
            var numRecords = JSONHelper.ToObject<List<LotteryRecord>>(hd_json.Value);
            LotteryResultFacade lrFacade = new LotteryResultFacade();
            lrFacade.AddLotteryResult(int.Parse(rbl_company.SelectedValue), numRecords);
        }
    }
}